﻿/**********************************************************************
*创建人：蔡水明
*创建时间：2018/11/15 16:43:12
*说明：<全局静态参数和方法>
*模板文件：C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ProjectTemplates\CSharp\Windows Root\Windows\2052\WindowsApplication
/**********************************************************************/
using System;
using System.IO;
using System.Windows.Forms;

using EasyExcel;

using Model.Data;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using Utilities;

namespace WssClient
{
    /// <summary>
    /// 全局变量
    /// </summary>
    public static class GVar
    {

        public static string ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
        /// <summary>
        /// 配置文件路径
        /// </summary>
        public static string relativeCfgPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Cfg");  //相对路径
        public static string absoluteCfgPath = "C:\\";  //绝对路径
        //public static string CfgPath = relativeCfgPath;
        public static string CfgPath = relativeCfgPath;

        //public static Item<Item.ZhongFu.ZhongFu, Item.ZhongFu.SalesStockOut> item = (new Item.ZhongFu.ZhongFu()).Init();
        //public static Item<Item.HengYuan.HengYuan, Item.HengYuan.SalesOrder> item = (new Item.HengYuan.HengYuan()).Init();
        public static Item<Item.XinDaXiang.XinDaXiang, Item.XinDaXiang.SalesStockOut> item = (new Item.XinDaXiang.XinDaXiang()).Init();

        public static int printIndex = 0;
        public static bool printMode = true;    //直接打印：true     预览：false

        public static string configFile = Path.Combine(CfgPath, "config.txt");

        /// <summary>
        /// 初始化信息
        /// </summary>
        public static void IniVar()
        {
            WssMsgHelper.Init();

            item.Builder();

            //LogHelper.Debug("======================================");
            //LogHelper.Debug($"configFile => {configFile}");
            //LogHelper.Debug($"excelFile => {Path.Combine(GVar.CfgPath, GVar.item.excelName)}");

            dynamic _printIndexDy = GetConfig("printIndex");
            int _printIndex = CommonHelper.FormatInt(_printIndexDy, -1);
            if(_printIndex < printIndex)
            {
                UpdatePrintIndex(printIndex);
            }
            else
            {
                printIndex = _printIndex;
            }

            dynamic _printModeDy = GetConfig("printMode");
            int _printMode = CommonHelper.FormatInt(_printModeDy, -1);
            if(_printMode == -1)
            {
                UpdatePrintMode(true);
            }
            else
            {
                printMode = _printMode == 1 ? true : false;
            }

            if(!Directory.Exists(CfgPath)) Directory.CreateDirectory(CfgPath);
            StartUp();

            if(string.IsNullOrEmpty(item.excelName)) return;

            ExcelDoc Excel = new ExcelDoc(item.excelName);
            Excel.Preheat();
        }
        /// <summary>
        /// 修改程序在注册表中的键值
        /// </summary>        
        private static void StartUp()
        {
            string path = Application.StartupPath;
            string keyName = path.Substring(path.LastIndexOf("\\") + 1);

            Microsoft.Win32.RegistryKey Rkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            if(Rkey == null)
            {
                Rkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
            }
            Rkey.SetValue(keyName, Application.ExecutablePath);

        }
        #region 显示消息
        public static void ShowMessage(string content)
        {
            if(content != null)
                MessageBox.Show(content, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        public static DialogResult ShowYNCMessage(string content)
        {
            return MessageBox.Show(content, "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
        }
        #endregion                    

        public static StreamWriter GetConfigWriter()
        {
            if(!File.Exists(configFile))
            {
                File.Create(configFile);
            }
            using(StreamWriter sw = new StreamWriter(configFile))
            {
                return sw;
            }
        }

        public static dynamic GetConfig()
        {
            if(!File.Exists(configFile))
            {
                using(FileStream stream = File.Create(configFile))
                {
                    stream.Close();
                }
            }
            using(StreamReader sr = new StreamReader(configFile))
            {
                string jsonStr = sr.ReadToEnd();
                dynamic jsonObj = JsonConvert.DeserializeObject<dynamic>(jsonStr);
                return jsonObj;
            }
        }

        public static dynamic GetConfig(string key)
        {
            dynamic o = GetConfig();
            dynamic value = CommonHelper.GetValue(o, key);
            return value;
        }

        public static void UpdateConfig(dynamic obj)
        {
            string json = JsonHelper.SerializeObject(obj);
            File.WriteAllText(configFile, json);
        }

        public static void UpdatePrintIndex(int index)
        {
            printIndex = index;
            dynamic obj = GetConfig();
            if(obj == null)
            {
                obj = new JObject();
            }
            obj["printIndex"] = index;
            UpdateConfig(obj);
        }

        public static void UpdatePrintMode(bool mode)
        {
            printMode = mode;
            dynamic obj = GetConfig();
            if(obj == null)
            {
                obj = new JObject();
            }
            obj["printMode"] = mode ? 1 : 0;
            UpdateConfig(obj);
        }
    }
}
